﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Arc.WinForms.Controls
{
    public static class ControlExtensions
    {
        public static IEnumerable<Control> GetControls<T>(this System.Windows.Forms.Control.ControlCollection Collection) where T : Control
        {
            List<Control> ChildControls = new List<Control>();
            foreach (Control ChildControl in Collection)
            {
                if (ChildControl is T)
                {
                    ChildControls.Add(ChildControl);
                }
                if (ChildControl.HasChildren)
                    ChildControls.AddRange(ChildControl.GetControls<T>());
            }
            return ChildControls.AsEnumerable();
        }
        public static IEnumerable<Control> GetControls<T>(this System.Windows.Forms.Control ParentControl) where T : Control
        {
            List<Control> ChildControls = new List<Control>();
            foreach (Control ChildControl in ParentControl.Controls)
            {
                if (ChildControl is T)
                {
                    ChildControls.Add(ChildControl);
                }
                if (ChildControl.HasChildren)
                    ChildControls.AddRange(ChildControl.GetControls<T>());
            }
            return ChildControls.AsEnumerable();
        }
    }
}
